<template>
    <div id="comic_diversity">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/novel_list' }">小说列表</el-breadcrumb-item>
            <el-breadcrumb-item>小说章节</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-form ref="form" label-width="80px" :inline="true" size="small">
                <el-form-item>
                    <el-button type="primary" size="small" @click="add">添加</el-button>
                </el-form-item>
            </el-form>
            <el-table
                    :data="tableData"
                    border
                    style="width: 100%">
                <el-table-column
                        prop="index"
                        label="序号">
                    <template slot-scope="scope">
                        {{scope.$index+1}}
                    </template>
                </el-table-column>
                <el-table-column
                        prop="title"
                        label="章节名称">
                </el-table-column>
                <el-table-column
                        prop="index"
                        label="章节封面">
                    <template slot-scope="scope">
                        <img :src="scope.row.coverpic" alt="" width="100">
                    </template>
                </el-table-column>
                <el-table-column
                        prop="chaps"
                        label="第几章节">
                </el-table-column>
                <el-table-column
                        prop="address"
                        label="操作"
                        width="430">
                    <template slot-scope="scope">
                        <p style="text-align: center">
                            <el-button type="primary" plain size="small" @click="edit(scope.row.id)">编辑</el-button>
                            <el-button type="warning" plain size="small" @click="remove(scope.row.id)">删除</el-button>
                        </p>
                    </template>
                </el-table-column>
            </el-table>
            <div class="el-pagination">
                <el-pagination
                        background
                        @size-change="changeSize"
                        @current-change="changeCurrent"
                        :current-page="params.pagenum"
                        :page-sizes="[10, 20, 30, 40]"
                        :page-size="params.pagesize"
                        :layout="this.$store.state.paginationLayout"
                        :total="params.totals">
                </el-pagination>
            </div>
        </el-card>
        <el-dialog :title="formTitle" :width="this.$store.state.dialogWidth":visible.sync="visible">
            <el-form ref="form" :label-width="this.$store.state.labelWidth" size="small">
                <el-form-item label="章节标题:">
                    <el-input v-model="form.title"></el-input>
                </el-form-item>
                <!--                <el-form-item label="章节图片:">-->
                <!--                    <el-upload-->
                <!--                            class="avatar-uploader"-->
                <!--                            action="upDataUrl"-->
                <!--                            :show-file-list="false"-->
                <!--                            :http-request="myUpload"-->
                <!--                            :before-upload="beforeAvatarUpload">-->
                <!--                        <img v-if="form.coverpic" :src="form.coverpic" class="avatar">-->
                <!--                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
                <!--                    </el-upload>-->
                <!--                </el-form-item>-->
                <el-form-item label="内容:">
                    <el-input
                            type="textarea"
                            :autosize="{ minRows: 9}"
                            placeholder="请输入内容"
                            class="textarea_input"
                            v-model="form.info">
                    </el-input>
                    <!--                    <quill-editor-->
                    <!--                            v-model="form.info"-->
                    <!--                            ref="myQuillEditor"-->
                    <!--                            :options="editorOption">-->
                    <!--                    </quill-editor>-->
                </el-form-item>
                <el-form-item>
                    <input type="file" accept=".txt" ref="inputer" @change="changeFile($event)">
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="visible = false" size="small">取 消</el-button>
                <el-button type="primary" @click="dialogAffirmEdit" size="small">确 定</el-button></span>
        </el-dialog>
    </div>
</template>

<script>
  import {isRealNum, uploadimg} from "../../../utils/common";
  import quillConfigText from '../../../utils/quill-text-config'

  export default {
    name: "comic_diversity",
    data() {
      return {
        params: {
          pagenum: 1,
          pagesize: 10,
          totals: 0
        },
        form: {
          title: '',
          info: '',
          coverpic: ''
        },
        tableData: [],
        fromTitle: '',
        visible: false,
        anid: '',//小说id
        id: '',//分集id
        editorOption: quillConfigText,
        formTitle: '',
      }
    },
    created() {
      this.anid = this.$route.query.id;
      this.getList()
    },
    methods: {
      changeFile(e) {
        let file = e.target.files[0];
        let formData = new FormData();
        formData.append('targetFile', file);
        this.$ajax({
          method: 'POST',
          url: 'admin/common/uploadAndGetData',
          headers: {
            'Content-Type': 'multipart/form-data',
            Authorization: 'Bearer ' + sessionStorage.getItem("token")
          },
          data: formData
        })
          .then(res => {
            console.log(res)
            this.form.info = res.message
          })
      },
      getList() {
        let data = {
          anid: this.anid,
          page: this.params.pagenum,
          page_size: this.params.pagesize,
        };
        this.$axios.post("admin/chapter/index", data).then((res) => {
          this.params.totals = res.data.count;
          this.tableData = res.data.list;
        });
      },
      // 每页显示多少条
      changeSize(val) {
        this.params.pagesize = val;
        this.getList()
      },
      // 当前显示的页数
      changeCurrent(val) {
        this.params.pagenum = val;
        this.getList()
      },
      add() {
        this.form = {
          title: '',
          info: '',
          coverpic: ''
        };
        this.formTitle='新增';
        this.id = '';
        this.visible = true;
      },
      edit(id) {
        this.id = id;
        this.fromTitle = '编辑';
        this.$axios.get('admin/chapter/' + id, {anid: this.anid}).then((res) => {
          let data = res.data;
          for (let key in data) {
            if (isRealNum(data[key])) {
              data[key] = data[key].toString()
            }
          }
          this.form = data;
          this.visible = true;
        });
      },
      remove(id) {
        this.$confirm('确定要进行操作, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$axios.del('admin/chapter/' + id, {anid: this.anid}).then((res) => {
            this.$message.success('删除成功!');
            this.getList();
          });
        }).catch(() => {

        });
      },
      dialogAffirmEdit() {
        if (this.id) {
          //编辑
          this.form = Object.assign({anid: this.anid}, this.form);
          this.$axios.put('admin/chapter/' + this.id, this.form).then((res) => {
            this.$message.success('成功');
            this.getList();
            this.visible = false;
          });
        } else {
          // 添加
          this.form = Object.assign({anid: this.anid}, this.form);
          this.$axios.post('admin/chapter', this.form).then((res) => {
            this.$message.success('成功');
            this.getList();
            this.visible = false;
          });
        }
      },
      myUpload(content) {
        uploadimg(content).then(res => {
          this.form.coverpic = res;
        })
      },
      beforeAvatarUpload(file) {
        var suffix = file.name.substring(file.name.lastIndexOf('.') + 1);
        let arr = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'pdf', 'JPG', 'JPEG', 'PNG', 'GIF', 'BMP', 'PDF']
        if (arr.indexOf(suffix) === -1) {
          this.$message.error('上传图片格式有误');
          return false
        }
      },
    }
  }
</script>

<style scoped>
</style>